require("../util.js");
require("../component/components.js");
require("../component/selfmediaflow.js");
require("../../jsMt/util.js");
var userInfo=JSON.parse(sessionStorage.getItem("userInfo"));
if(!userInfo){
    location.href="/loginIndex"
}
var vm = new Vue({
    el:"#myVideo",
    data:{
        name:"",
        type:"",
        videoCategory:{},//视频分类
        loadFlag:false,
        title:"",
        categoryId:"",
        list:{
            loginUid:userInfo.id,
            // loginUid:1,
            pageNumber:1,
            categoryId:""

        },
        // isScreen:"",
        noticeList:{},//列表
        noticeList1:[],
        noticeList2:[],
        number:"",
        num:"",
        index:2
    },
    mounted:function(){
        this.searchObject = this.getQueryStringArgs();
        // this.productId=this.searchObject&&this.searchObject.productId;
        this.type=this.searchObject&&this.searchObject.type;
        if(this.type==1||this.type==3||this.type==4){
            this.myVideoCategory();
            if(this.type==1){
                this.name="视频";
                this.number=3;
                this.num=7;
                this.index=2;
            }else if(this.type==3){
                this.name="收藏";
                this.number=5;
                this.num=5;
            }else if(this.type==4){
                this.name="点赞";
                this.num=5;
            }

        }else if(this.type==2){ //我的游记
            this.myVideoList();
            this.name="游记";
            this.number=4;
            this.num=6;
            this.index=2;
        }

    },
    methods:{
        //后退
        getBack:function(){
            history.go(-1);
        },
        bgimg:function(url){
            url = url?url:"";
            return {
                "background-image":'url("'+url+'"),url("")'
            }
        },
        /*获取查询字符对象*/
        getQueryStringArgs: function () {
            //取得查询字符串并去掉开头的问号
            var qs = (location.search.length > 0 ? location.search.substring(1) : ""),
                args = {},
                items = qs.length ? qs.split("&") : [],
                item = null,
                name = null,
                value = null,
                len = items.length;
            for (var i = 0; i < len; i++) {
                item = items[i].split("=");
                name = item[0];
                value = item[1];
                if (name.length) {
                    args[name] = value;
                }
            }
            return args;
        },
        //我的视频分类
        myVideoCategory:function(){
            var that=this;
            var data={
                loginUid:that.list.loginUid
            };
            var url;
            if(that.type==1){
                url="/happyi/myVideoCategory";
            }else if(that.type==3){
                url="/happyi/myCollectionCategory";
            }else if(that.type==4){
                url="/happyi/myPraiseCategory";
            }
            that.ajax({url:url,data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.videoCategory=b.json.data;
                    that.myVideoList();
                }else{
                    vm.$refs.prompt.toast("获取分类失败！");
                }
            });
        },
        myTab:function(id){
            var that=this;
            that.list.pageNumber=1;
            that.noticeList={};
            that.noticeList1=[];
            that.noticeList2=[];
            if(id){
                that.list.categoryId=id;
            }else{
                that.list.categoryId="";
            }
            window.scrollTo(0,0);
            that.myVideoList();
        },
        //我的视频(游记)列表
        myVideoList:function(){
            var that=this;
            console.log(that.list.pageNumber);
            var url;
            if(that.type==1){
                url="/happyi/myVideoList";
            }else if(that.type==2){
                url="/happyi/myTravels";
            }else if(that.type==3){
                url="/happyi/myCollection";
            }else if(that.type==4){
                url="/happyi/myPraise";
            }
            this.ajax({url:url,data:that.list,dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.noticeList=b.json.data;
                    that.noticeList1=that.noticeList.list;
                    if(that.noticeList.pageNumber>1){
                        if(that.noticeList.list.length>0) {
                            that.noticeList2 = that.noticeList2.concat(b.json.data.list);
                        }
                    }else{
                        that.noticeList2=that.noticeList1;
                    }
                    vm.loadFlag=false;
                }else{
                    that.noticeList={};
                    that.noticeList1=[];
                    that.noticeList2=[];
                    vm.$refs.prompt.toast("获取列表失败！");
                }
            });
        },
        getIsShow:function(num){
            this.index=num;
        },
        getDeleteVideo:function(videoId){
            var that=this;
            var data={
                loginUid:userInfo.loginUid,
                videoId:videoId
            };
            console.log(data)
            that.ajax({url:"/happyi/user/deleteVideo",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0){
                    vm.$refs.prompt.toast(b.json.msg);
                    that.myVideoList();
                }else{
                    vm.$refs.prompt.toast("删除失败！");
                }
            });
        }
        /*//跳转
        getNext:function(item){
            console.log(item)
            if(this.type==1){
                if(item.type==1){   // 1自媒体 2企业广告3城市广告4传统文化
                    location.href="/see/selfMediaVd?id="+item.id+"&uid="+userInfo.id+"&sourceType=1";
                }else if(item.type==2){
                    location.href="/see/adDetails?id="+item.id+"&uid="+item.uid;
                }else if(item.type==3){
                    location.href="/cityAd/cityAdVideo?cityAdvId="+item.id;
                }else if(item.type==4){

                }
            }else if(num==2){

            }
        }*/
    }
    ,watch:{
        noticeList2:function(){
            // debugger;
            if(this.noticeList2.length!=0){
                this.$nextTick(function(){
                    fmasonry();
                    $("img.lazy").lazyload({
                        failurelimit:10,
                        load:fmasonry
                    });
                    console.log($(".selfMedia-tabCent").height());
                    // if(getWindowHeight())

                });
            }

        }

    }
});
//文档高度
function getDocumentTop() {
    var scrollTop = 0, bodyScrollTop = 0, documentScrollTop = 0;
    if (document.body) {
        bodyScrollTop = document.body.scrollTop;
    }
    if (document.documentElement) {
        documentScrollTop = document.documentElement.scrollTop;
    }
    scrollTop = (bodyScrollTop - documentScrollTop > 0) ? bodyScrollTop : documentScrollTop;    return scrollTop;
}
//可视窗口高度
function getWindowHeight() {
    var windowHeight = 0;
    if (document.compatMode == "CSS1Compat") {
        windowHeight = document.documentElement.clientHeight;
    } else {
        windowHeight = document.body.clientHeight;
    }
    return windowHeight;
}
//滚动条滚动高度
function getScrollHeight() {
    var scrollHeight = 0, bodyScrollHeight = 0, documentScrollHeight = 0;
    if (document.body) {
        bodyScrollHeight = document.body.scrollHeight;
    }
    if (document.documentElement) {
        documentScrollHeight = document.documentElement.scrollHeight;
    }
    scrollHeight = (bodyScrollHeight - documentScrollHeight > 0) ? bodyScrollHeight : documentScrollHeight;
    return scrollHeight;

}
window.onscroll=function(){
    var loadFlag= vm.noticeList.lastPage;
    //监听事件内容
    if(getScrollHeight()==getWindowHeight() + getDocumentTop()){
        //当滚动条到底时,这里是触发内容
        if(!loadFlag){
            vm.loadFlag=true;
            vm.list.pageNumber++;
            vm.myVideoList();
        }else{
            vm.$refs.prompt.toast("没有更多了！")
        }
    }
};
function fmasonry() {
    minigrid('.selfMedia-tabCent', '.selfMedia-list',9);
    // $(".selfMedia-tabCent").masonry({
    //     gutterWidth: 10,
    //     itemSelector: '.selfMedia-list',
    //     isAnimated: true,
    //     isFitWidth:true
    // });
}
function lazyload(){
    $("img.lazy").lazyload();
}